<!-- 任务表单组件 -->
<template>
  <view class="task-form">
    <view class="form-header">
      <text class="form-title">{{ isEdit ? '编辑任务' : '新建任务' }}</text>
      <view class="close-btn" @click="onCancel">
        <text class="close-icon">×</text>
      </view>
    </view>
    
    <view class="form-content">
      <!-- 任务标题输入框 -->
      <view class="form-item">
        <text class="form-label">任务标题</text>
        <input 
          class="form-input" 
          v-model="formData.title" 
          placeholder="请输入任务标题"
          maxlength="50"
          @blur="validateTitle"
        />
        <text class="error-tip" v-if="errors.title">{{ errors.title }}</text>
      </view>
      
      <!-- 任务描述输入框 -->
      <view class="form-item">
        <text class="form-label">任务描述</text>
        <textarea 
          class="form-textarea" 
          v-model="formData.description" 
          placeholder="请输入任务描述"
          maxlength="200"
        />
      </view>
      
      <!-- 优先级选择 -->
      <view class="form-item">
        <text class="form-label">优先级</text>
        <view class="priority-selector">
          <view 
            v-for="(item, index) in priorityOptions" 
            :key="index"
            class="priority-item" 
            :class="[`priority-${item.value}`, {'priority-active': formData.priority === item.value}]"
            @click="selectPriority(item.value)"
          >
            <text class="priority-text">{{ item.label }}</text>
          </view>
        </view>
      </view>
      
      <!-- 截止日期选择 -->
      <view class="form-item">
        <text class="form-label">截止日期</text>
        <view class="date-picker" @click="showDatePicker = true">
          <text v-if="formData.deadline" class="date-text">{{ formatDate(formData.deadline) }}</text>
          <text v-else class="date-placeholder">选择截止日期</text>
          <u-icon name="calendar" size="20" color="#999"></u-icon>
        </view>
      </view>
      
      <!-- 标签输入 -->
      <view class="form-item">
        <text class="form-label">标签</text>
        <view class="tags-input">
          <view class="tag-item" v-for="(tag, index) in formData.tags" :key="index">
            <text class="tag-text">{{ tag }}</text>
            <text class="tag-delete" @click="removeTag(index)">×</text>
          </view>
          <input 
            class="tag-input" 
            v-model="newTag" 
            placeholder="输入标签后按回车添加" 
            @confirm="addTag"
          />
        </view>
      </view>
    </view>
    
    <view class="form-footer">
      <button class="cancel-btn" @click="onCancel">取消</button>
      <button class="submit-btn" @click="submitForm">{{ isEdit ? '保存修改' : '创建任务' }}</button>
    </view>
    
    <!-- 日期选择器 -->
    <u-calendar 
      :show="showDatePicker"
      @confirm="onDateConfirm"
      @close="showDatePicker = false"
      mode="single"
      :min-date="new Date().getTime()"
    ></u-calendar>
  </view>
</template>

<script>
export default {
  name: 'TaskForm',
  props: {
    // 编辑模式下传入的任务数据
    taskData: {
      type: Object,
      default: () => ({})
    },
    // 是否为编辑模式
    isEdit: {
      type: Boolean,
      default: false
    },
    // 当前分组ID
    groupId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: {
        title: '',
        description: '',
        priority: 'medium',
        tags: [],
        deadline: 0,
        status: 'todo'
      },
      errors: {
        title: ''
      },
      priorityOptions: [
        { label: '高', value: 'high' },
        { label: '中', value: 'medium' },
        { label: '低', value: 'low' },
        { label: '无', value: 'none' }
      ],
      showDatePicker: false,
      newTag: ''
    };
  },
  created() {
    // 如果是编辑模式，初始化表单数据
    if (this.isEdit && this.taskData) {
      this.formData.title = this.taskData.title || '';
      this.formData.description = this.taskData.description || '';
      this.formData.priority = this.taskData.priority || 'medium';
      this.formData.tags = [...(this.taskData.tags || [])];
      this.formData.deadline = this.taskData.deadline || 0;
      this.formData.status = this.taskData.status || 'todo';
    }
  },
  methods: {
    // 选择优先级
    selectPriority(priority) {
      this.formData.priority = priority;
    },
    
    // 验证标题
    validateTitle() {
      if (!this.formData.title.trim()) {
        this.errors.title = '请输入任务标题';
        return false;
      } else if (this.formData.title.length > 50) {
        this.errors.title = '任务标题不能超过50个字符';
        return false;
      } else {
        this.errors.title = '';
        return true;
      }
    },
    
    // 添加标签
    addTag() {
      const tag = this.newTag.trim();
      if (tag && !this.formData.tags.includes(tag)) {
        this.formData.tags.push(tag);
      }
      this.newTag = '';
    },
    
    // 移除标签
    removeTag(index) {
      this.formData.tags.splice(index, 1);
    },
    
    // 日期确认
    onDateConfirm(e) {
      this.formData.deadline = e.timestamp;
      this.showDatePicker = false;
    },
    
    // 格式化日期
    formatDate(timestamp) {
      if (!timestamp) return '';
      
      try {
        const date = new Date(timestamp);
        return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;
      } catch (error) {
        console.error('格式化日期失败:', error);
        return '';
      }
    },
    
    // 提交表单
    submitForm() {
      if (!this.validateTitle()) {
        return;
      }
      
      const taskData = {
        title: this.formData.title.trim(),
        description: this.formData.description.trim(),
        priority: this.formData.priority,
        tags: [...this.formData.tags],
        deadline: this.formData.deadline,
        status: this.formData.status
      };
      
      // 如果是编辑模式，添加ID
      if (this.isEdit && this.taskData && this.taskData.id) {
        taskData.id = this.taskData.id;
        taskData.groupId = this.taskData.groupId;
      } else {
        // 新建模式，添加分组ID
        taskData.groupId = this.groupId;
      }
      
      this.$emit('submit', taskData);
    },
    
    // 取消操作
    onCancel() {
      this.$emit('cancel');
    }
  }
}
</script>

<style lang="scss" scoped>
.task-form {
  background-color: #ffffff;
  border-radius: 8px;
  overflow: hidden;
  width: 100%;
  max-width: 600rpx;
}

.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #f0f0f0;
}

.form-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.close-btn {
  width: 24px;
  height: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
}

.close-icon {
  font-size: 24px;
  color: #999;
}

.form-content {
  padding: 15px;
  max-height: 60vh;
  overflow-y: auto;
}

.form-item {
  margin-bottom: 15px;
}

.form-label {
  display: block;
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.form-input {
  width: 100%;
  height: 40px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 10px;
  font-size: 14px;
  color: #333;
  box-sizing: border-box;
}

.form-textarea {
  width: 100%;
  height: 80px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 10px;
  font-size: 14px;
  color: #333;
  box-sizing: border-box;
}

.error-tip {
  font-size: 12px;
  color: #ff4d4f;
  margin-top: 5px;
}

.priority-selector {
  display: flex;
  margin-top: 10px;
}

.priority-item {
  flex: 1;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #ddd;
  margin-right: 10px;
  border-radius: 4px;
  
  &:last-child {
    margin-right: 0;
  }
  
  &.priority-high {
    border-color: #ff4d4f;
    
    &.priority-active {
      background-color: #ff4d4f;
      .priority-text {
        color: #fff;
      }
    }
  }
  
  &.priority-medium {
    border-color: #faad14;
    
    &.priority-active {
      background-color: #faad14;
      .priority-text {
        color: #fff;
      }
    }
  }
  
  &.priority-low {
    border-color: #52c41a;
    
    &.priority-active {
      background-color: #52c41a;
      .priority-text {
        color: #fff;
      }
    }
  }
  
  &.priority-none {
    border-color: #d9d9d9;
    
    &.priority-active {
      background-color: #d9d9d9;
      .priority-text {
        color: #fff;
      }
    }
  }
}

.priority-text {
  font-size: 14px;
}

.date-picker {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 40px;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0 10px;
  box-sizing: border-box;
}

.date-text {
  font-size: 14px;
  color: #333;
}

.date-placeholder {
  font-size: 14px;
  color: #999;
}

.tags-input {
  display: flex;
  flex-wrap: wrap;
  padding: 5px;
  border: 1px solid #ddd;
  border-radius: 4px;
  min-height: 40px;
}

.tag-item {
  display: flex;
  align-items: center;
  background-color: #f0f0f0;
  border-radius: 4px;
  padding: 2px 8px;
  margin-right: 8px;
  margin-bottom: 5px;
}

.tag-text {
  font-size: 12px;
  color: #333;
}

.tag-delete {
  font-size: 14px;
  color: #999;
  margin-left: 5px;
}

.tag-input {
  flex: 1;
  min-width: 80px;
  height: 30px;
  font-size: 14px;
  border: none;
  outline: none;
}

.form-footer {
  padding: 15px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.submit-btn {
  flex: 3;
  height: 40px;
  background-color: #4285f4;
  color: #fff;
  border: none;
  border-radius: 20px;
  font-size: 16px;
  box-shadow: 0 2px 5px rgba(66, 133, 244, 0.3);
}

.cancel-btn {
  flex: 2;
  height: 40px;
  background: #f5f5f5;
  color: #666;
  border: none;
  border-radius: 20px;
  font-size: 16px;
  margin-right: 10px;
}
</style> 